import Vue from 'vue'
import VueRouter from 'vue-router'

import * as hooks from './hooks/showFooterHook'
import * as pvHook from './hooks/pvHook'
import * as loginHook from './hooks/checkLoginRoute'

// 导入对应的路由模块
// import films from './routes/films'
// import cinemas from './routes/cinemas'
// import news from './routes/news'
// import center from './routes/center'
// import notfound from './routes/notfound'
// import home from './routes/home'

// 自动导入routes中的路由模块 webpack中提供一个api方法，可以自动去检索对应目录下面的模块
// 参数1：检索目录
// 参数2：是否递归对应的检索目录  true递归  false不递归  一般要求是false
// 参数3：指定最终要的文件格式 正则 .js
// 返回值  模块函数
const moduleFn = require.context('./routes', false, /\.js$/i)
// 得到当前routes下面的文件列表
const routes = moduleFn.keys().reduce((prev, curr) => {
  let module = moduleFn(curr).default
  if (Array.isArray(module)) {
    prev.push(...module)
  } else {
    prev.push(module)
  }
  return prev
}, [])

Vue.use(VueRouter)

// const routes = [...films, ...cinemas, ...news, ...center, home, notfound]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

Object.keys(hooks).forEach(hookname => router.beforeEach(hooks[hookname]))
Object.keys(pvHook).forEach(hookname => router.beforeEach(pvHook[hookname]))
Object.keys(loginHook).forEach(hookname => router.beforeEach(loginHook[hookname]))

export default router
